package com.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.util.CpachaUtil;
/**
 *验证码servlet
 */
public class CaptchaServlet extends HttpServlet {

	private static final long serialVersionUID = 4919529414762301338L;
	public void doGet(HttpServletRequest request,HttpServletResponse reponse) throws IOException{
		doPost(request, reponse);
	}
	public void doPost(HttpServletRequest request,HttpServletResponse reponse) throws IOException{
		String method = request.getParameter("method");
		if("loginCaptcha".equals(method)){
			generateloginCaptcha(request, reponse);
			return;
		}
		reponse.getWriter().write("error method");
	}
	private void generateloginCaptcha(HttpServletRequest request,HttpServletResponse rep) throws IOException{
		CpachaUtil cpachaUtil = new CpachaUtil();
		//生成验证码
		String generatorVCode = cpachaUtil.generatorVCode();
		//获取session中的值
		request.getSession().setAttribute("loginCaptcha", generatorVCode);
		//画验证码
		BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
		//输出图片
		ImageIO.write(generatorRotateVCodeImage, "jpg", rep.getOutputStream());
	}
}
